<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="学号" prop="studentNo">
        <el-input
          v-model="queryParams.studentNo"
          placeholder="请输入学号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学生姓名" prop="studentName">
        <el-input
          v-model="queryParams.studentName"
          placeholder="请输入学生姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学生电话" prop="cellNo">
        <el-input
          v-model="queryParams.cellNo"
          placeholder="请输入学生电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="班型" prop="modelId">
        <el-select v-model="queryParams.modelId" placeholder="请选择班型" clearable size="small">
          <el-option
                v-for="model in modelOptions"
                :key="model.modelId"
                :label="model.modelName"
                :value="model.modelId"
              />
        </el-select>
      </el-form-item>
      <el-form-item label="报名类型" prop="signType">
        <el-select v-model="queryParams.signType" placeholder="请选择状态" clearable size="small">
          <el-option
                v-for="dict in studentTypeOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
        </el-select>
      </el-form-item>
      <el-form-item label="报名状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option
                v-for="dict in statusOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
        </el-select>
      </el-form-item>
      <!--
      <el-form-item label="报名时间" prop="signTime">
        <el-date-picker clearable size="small" style="width: 200px"
          v-model="queryParams.signTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择报名时间">
        </el-date-picker>
      </el-form-item>
      -->
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['edu:sign:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['edu:sign:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['edu:sign:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['edu:sign:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="signList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!--<el-table-column label="报名ID" align="center" prop="signId" />-->
      <el-table-column label="学号" align="center" prop="studentNo" />
      <el-table-column label="学生姓名" align="center" prop="studentName" />
      <el-table-column label="电话" align="center" prop="cellNo" />
      <el-table-column label="班型" align="center" prop="modelName" />
      <el-table-column label="报名类型" :formatter="studentTypeFormat" align="center" prop="signType" />
      <!--<el-table-column label="进度阶段标志" align="center" prop="progressFlag" />
      <el-table-column label="支付流水ID" align="center" prop="paySerialId" />
      <el-table-column label="支付流水单号" align="center" prop="paySerialNo" />-->
      <el-table-column label="报名状态" :formatter="statusFormat" align="center" prop="status" />
      <el-table-column label="报名时间" align="center" prop="signTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.signTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!--<el-table-column label="备注" align="center" prop="remark" />-->
      <el-table-column label="操作" align="center" width="180" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['edu:sign:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleQryProgress(scope.row)"
            v-hasPermi="['edu:sign:query']"
          >进度</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['edu:sign:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改学生报名信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-tabs v-model="activeName" @tab-click="handleTabChange">
          <el-tab-pane label="基本信息" name="first">
          <!--
          <el-form-item label="学生ID" prop="studentId">
            <el-input v-model="form.studentId" placeholder="请输入学生ID" />
          </el-form-item>
          -->
          <el-form-item label="班型" prop="modelId">
            <el-select v-model="form.modelId" placeholder="请选择班型" clearable size="small">
            <el-option
                  v-for="model in modelOptions"
                  :key="model.modelId"
                  :label="model.modelName"
                  :value="model.modelId"
                />
          </el-select>
          </el-form-item>
          <el-form-item label="报名类型" prop="signType">
            <el-select v-model="form.signType" placeholder="请选择状态" clearable size="small">
            <el-option
                  v-for="dict in studentTypeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
          </el-select>
          </el-form-item>
          <!--
          <el-form-item label="进度阶段标志" prop="progressFlag">
            <el-input v-model="form.progressFlag" placeholder="请输入进度阶段标志" />
          </el-form-item>
          
          <el-form-item label="支付流水ID" prop="paySerialId">
            <el-input v-model="form.paySerialId" placeholder="请输入支付流水ID" />
          </el-form-item>
          -->
          <el-form-item label="支付流水" prop="paySerialNo">
            <el-input v-model="form.paySerialNo" placeholder="请输入支付流水单号" />
          </el-form-item>
          <el-form-item label="报名状态">
            <el-radio-group v-model="form.status">
                      <el-radio
                        v-for="dict in statusOptions"
                        :key="dict.dictValue"
                        :label="dict.dictValue"
                      >{{dict.dictLabel}}</el-radio>
                    </el-radio-group>
          </el-form-item>
          <el-form-item label="报名时间" prop="signTime">
            <el-date-picker clearable size="small" style="width: 200px"
              v-model="form.signTime"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="选择报名时间">
            </el-date-picker>
          </el-form-item>
          <!--
          <el-form-item label="删除标志" prop="delFlag">
            <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
          </el-form-item>
          -->
          </el-tab-pane>
          <el-tab-pane label="进度信息" name="second">
            <el-table v-loading="loading" :data="signProgressList">
              <!--<el-table-column type="selection" width="55" align="center" />
              <el-table-column label="报名ID" align="center" prop="progressId" />
              <el-table-column label="报名ID" align="center" prop="signId" />-->
              <el-table-column label="阶段完成时间" align="center" prop="progressTime">
                <template slot-scope="scope">
                  <span>{{ parseTime(scope.row.progressTime, '{y}-{m}-{d}') }}</span>
                </template>
              </el-table-column>
              <el-table-column label="进度阶段" :formatter="signStatusFormat" align="center" prop="progressFlag" />
            </el-table>
          </el-tab-pane>
        </el-tabs>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSign, getSign, delSign, addSign, updateSign, exportSign } from "@/api/edu/sign";
import { listAllBySignId } from "@/api/edu/signProgress";
import { listAllClassModel } from "@/api/edu/classModel";
export default {
  name: "Sign",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学生报名信息表格数据
      signList: [],
      statusOptions: [],
      modelOptions: [],
      studentTypeOptions: [],
      activeName: "first",
      signProgressList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        studentId: null,
        studentNo: null,
        studentName: null,
        cellNo: null,
        modelId: null,
        signType: null,
        progressFlag: null,
        paySerialId: null,
        paySerialNo: null,
        status: null,
        signTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        studentId: [
          { required: true, message: "学生ID不能为空", trigger: "blur" }
        ],
        modelId: [
          { required: true, message: "班型ID不能为空", trigger: "blur" }
        ],
        paySerialId: [
          { required: true, message: "支付流水ID不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getDicts("edu_sign_status").then(response => {
      this.statusOptions = response.data;
    });
    this.getDicts("edu_student_type").then(response => {
      this.studentTypeOptions = response.data;
    });
    
    this.getClassModel()
    const studentId = this.$route.params && this.$route.params.studentId;
    this.queryParams.studentId = studentId
    this.getList();
  },
  methods: {
    // 状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    studentTypeFormat(row, column) {
      return this.selectDictLabel(this.studentTypeOptions, row.signType);
    },
    signStatusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.progressFlag);
    },
    getClassModel() {
      listAllClassModel().then(response => {
        this.modelOptions = response.data
      })
    },
    /** 查询学生报名信息列表 */
    getList() {
      this.loading = true;
      listSign(this.queryParams).then(response => {
        this.signList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        signId: null,
        studentId: null,
        studentName: null,
        modelId: null,
        modelName: null,
        signType: null,
        progressFlag: null,
        paySerialId: null,
        paySerialNo: null,
        status: "0",
        signTime: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.signId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加学生报名信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const signId = row.signId || this.ids
      getSign(signId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改学生报名信息";
      });
    },
    handleQryProgress(row) {
      this.reset();
      getSign(row.signId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "查看学生报名信息";
        this.activeName = "second"
        this.handleTabChange()
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.signId != null) {
            updateSign(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSign(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const signIds = row.signId || this.ids;
      this.$confirm('是否确认删除学生报名信息编号为"' + signIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delSign(signIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有学生报名信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportSign(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    },
    handleTabChange() {
      if("second" === this.activeName) {
        if(this.form.signId) {
          listAllBySignId(this.form.signId).then(response => {
            this.signProgressList = response.data
            //console.log(JSON.stringify(this.signProgressList))
          })
        }
        
      }
    }
  }
};
</script>
